﻿namespace WebPacker
{
	using System.Text;
	using System.Web;
	using WebPacker.Helpers;
	using WebPacker.Logger;
	using WebPacker.Packs;

	public static class WebPackerHtml
	{
		private static HttpContextBase Context
		{
			get { return HttpContext.Current != null ? new HttpContextWrapper(HttpContext.Current) : null; }
		}

		private static IPackRouter Router
		{
			get { return WebPackerRouter.Instance; }
		}

		public static string ScriptPack(string packName)
		{
			return ResourcePack(PackType.JavaScript, packName);
		}

		public static string ScriptPack(string[] resources)
		{
			return ResourcePack(PackType.JavaScript, resources);
		}

		public static string CssPack(string packName)
		{
			return ResourcePack(PackType.Css, packName);
		}

		public static string CssPack(string[] resources)
		{
			return ResourcePack(PackType.Css, resources);
		}

		private static string ResourcePack(PackType packType, string packName)
		{
			if (Router == null)
			{
				throw new WebPackerException("WebPacker isn't initialized.");
			}

			if (Router.Enabled)
			{
				var url = Router.GetUrl(Context, packType, packName);

				if (url != null)
				{
					return ResourcePackTag(packType, url);
				}
			}

			var pack = Router.GetPack(packType, packName);

			if (pack == null)
			{
				throw new WebPackerException(string.Format("Pack {0} isn't found", LogHelper.ToString(packType, packName)));
			}

			string[] urls = new string[pack.Resources.Length];

			for (var i = 0; i < urls.Length; i++)
			{
				urls[i] = pack.Resources[i].Name;
			}

			return ResourceTag(packType, urls);
		}

		private static string ResourcePack(PackType packType, string[] resources)
		{
			if (Router != null && Router.Enabled)
			{
				var url = Router.GetUrl(Context, packType, resources);

				if (url != null)
				{
					return ResourcePackTag(packType, url);
				}
			}

			return ResourceTag(packType, resources);
		}

		private static string ResourcePackTag(PackType packType, string url)
		{
			var builder = new StringBuilder(url.Length + 50);

			if (packType == PackType.JavaScript)
			{
				builder.Append("<script type=\"text/javascript\" src=\"").Append(url).Append("\"></script>");
			}
            else if (packType == PackType.Css)
            {
                builder.Append("<link href=\"").Append(url).Append("\" rel=\"stylesheet\" type=\"text/css\" />");
            }

			return builder.ToString();
		}

		private static string ResourceTag(PackType packType, string[] paths)
		{
			var builder = new StringBuilder(paths.Length * 100);

			foreach (var path in paths)
			{
                var url = VirtualPathHelper.ToVirtualPath(path);

				if (packType == PackType.JavaScript)
				{
                    builder.Append("<script type=\"text/javascript\" src=\"").Append(url).Append("\"></script>");
				}
				else if (packType == PackType.Css)
				{
					builder.Append("<link href=\"").Append(url).Append("\" rel=\"stylesheet\" type=\"text/css\" />");
				}
			}

			return builder.ToString();
		}
	}
}
